package cn.jdy.task.service.classify;


import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.jdy.common.util.SMSUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.CRateOrder;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.entity.PointRecord;
import cn.jdy.plugins.service.CRateOrderService;
import cn.jdy.plugins.service.MemberService;
import cn.jdy.plugins.service.PointRecordService;
import cn.jdy.plugins.service.ProductxService;
import cn.jdy.plugins.service.WebsiteService;
import cn.jdy.task.service.TaskGroup;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.mysql.jdbc.StringUtils;

@TaskGroup("积分情况")
public class WebsiteCancel implements Job {
	@Autowired
	private WebsiteService websiteService;
	@Autowired
	private CRateOrderService cRateOrderService;
	@Autowired
	private ProductxService productxService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PointRecordService pointRecordService;
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			System.out.println("执行了一次");
		
				List<Map<String,Object>>	listsa=cRateOrderService.cRateCountQuery("");//查询订单
			 
				if(listsa.size()>0){
					
			
				    for (Map<String, Object> map : listsa) {
				    	  String istqsr=map.get("istqsr")+"";//订单时间
				    	
				    	if(istqsr.equals("1")){  //固定标
				    	
				    			int sj=Integer.valueOf(map.get("rzts")+"");//入住时间
					    		String createTime=map.get("createTime")+"";//订单时间
					    		String dqtime=map.get("dqtime")+"";///到期时间
					     		SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
					     		Calendar calendar = Calendar.getInstance();
					     		Date dqs=   s.parse(dqtime); //到期时间
					     		calendar.setTime(dqs);
					     		calendar.add(Calendar.DATE, -1);//到期时间减一天在相比
					        	calendar.getTime();
					        	Date jytdq=  calendar.getTime();
					        	Date dq= new Date(); //当前时间
					      		String spmz=  map.get("productxname")+"";//商品名字
					    		String mobile=  map.get("mobile")+"";//商品名字
					 
						 if(dq.getTime()<jytdq.getTime()){
							    double ta=Double.valueOf(map.get("total")+"");//订单价格
							    double lv=Double.valueOf(map.get("lv")+"");//订单利率
							    double sylv=0;
							    if(!StringUtils.isNullOrEmpty(map.get("sylv").toString())){
								    sylv=Double.valueOf(map.get("sylv")+"");//是否首页加利率
							    } 
							    PointRecord pr=new PointRecord();
							    pr.setOrderNo(map.get("orderNo")+"");
							    pr.setMch_id(map.get("mch_id")+"");
							    pr.setMemberid(map.get("memberid")+"");
							    pr.setUsername(map.get("username")+"");
							    pr.setRpType("3"); 
							    pr.setProductxid(map.get("productxid")+""); 
							    pr.setProductxname(map.get("productxname")+""); 
						  		pr.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
							    pr.setType("0"); 
							    double de=0;
							    if(sylv!=0){
							    	de=ta*(lv+sylv)/365;
							    }else{
							    	de=ta*lv/365;
							    }
							    pr.setTotal(de);
							    pointRecordService.save(pr);
							    
							    CRateOrder co=  cRateOrderService.findOne("orderNo",pr.getOrderNo());
							    co.setYjjf(co.getYjjf()+Double.valueOf(pr.getTotal()));
							    cRateOrderService.update(co);
							    
							    Member  mb=  memberService.find(co.getMemberid());
							    mb.setScore(mb.getScore()+Double.valueOf(pr.getTotal()));
							    memberService.update(mb);
						 }else{ //到期发信息
								try {
									SMSUtil su=new SMSUtil(); 
									su.smsxwxx(mobile+"","你预定的"+mobile+"已经到期，请转卖。");
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
						 }//
				     	}else{ //随时标
				     					String createTime=map.get("createTime")+"";//订单时间
							    	    String spmz=  map.get("productxname")+"";//商品名字
							    	    String mobile=  map.get("mobile")+"";//商品名字
									    double ta=Double.valueOf(map.get("total")+"");//订单价格
									    double lv=Double.valueOf(map.get("lv")+"");//订单利率
									    double sylv=0;
									    if(!StringUtils.isNullOrEmpty(map.get("sylv").toString())){
										    sylv=Double.valueOf(map.get("sylv")+"");//是否首页加利率
									    } 
									    
									    PointRecord pr=new PointRecord();
									    pr.setOrderNo(map.get("orderNo")+"");
									    pr.setMch_id(map.get("mch_id")+"");
									    pr.setMemberid(map.get("memberid")+"");
									    pr.setUsername(map.get("username")+"");
									    pr.setRpType("3"); 
									    pr.setProductxid(map.get("productxid")+""); 
									    pr.setProductxname(map.get("productxname")+""); 
								  		pr.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
									    pr.setType("0"); 
									    double de=0;
									    if(sylv!=0){
									    	de=ta*(lv+sylv);
									    }else{
									    	de=ta*lv;
									    }
									    pr.setTotal(de);
									    pointRecordService.save(pr);
									    
									    CRateOrder co=  cRateOrderService.findOne("orderNo",pr.getOrderNo());
									    co.setYjjf(co.getYjjf()+Double.valueOf(pr.getTotal()));
									    cRateOrderService.update(co);
									    
									    Member  mb=  memberService.find(co.getMemberid());
									    mb.setScore(mb.getScore()+Double.valueOf(pr.getTotal()));
									    memberService.update(mb);
				     	
					}
					}
			}
			//查询所有用户
			
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "invoke", e);
		}
	}
public static void main(String[] args) throws ParseException {
 
       
	   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");//设置日期格式
       System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
       
	    
	     SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
	     Calendar calendar = Calendar.getInstance();
	     Date dqs=   s.parse("2018-10-13"); //到期时间
	        calendar.setTime(dqs);
	        calendar.add(Calendar.DATE, -1);
	        s.format(calendar.getTime());

	     Date dq= new Date(); //当前时间
	     
	     System.out.println(dq.getTime()<dqs.getTime());
}
}
